<?php
set_time_limit(0); //防止执行超时
ob_end_clean();   //清空并关闭输出缓冲区

if(!isset($_GET["id"])){
    echo "pls input id!";
    return;
}
if(!isset($_GET["p"])){
    $_GET["p"] = 3;
}

include("base_function.php");
include("base/db.php");

function analyse(&$html,$catalog_id,$max = -1)
{
    $reg = '|<div class="s225">[\w\W]*?<a href="(.*?)"|';
    preg_match_all ($reg,  
        $html,
        $out, PREG_PATTERN_ORDER);
    //$goog_urls = array;
    $index = 0;
    foreach($out[1] as $url){
        $goog_url["catalog_id"] = $catalog_id;
        $goog_url["url"] = $url;
        $goog_urls[] = $goog_url;
        if($max == -1 || $max > $index++){
            //exec_db ("delete from pm_product where url='$url'");
            exec_db ( "insert into pm_product (catalog_id,url) values ('$catalog_id','$url')\r\n");
        }
    }
}
function doit()
{
    open_db();
    $sub_catalogs = query_db("select * from pm_sub_catalog where id = ".$_GET["id"],"id");
    $sub_catalog = $sub_catalogs[$_GET["id"]];
//    print_r($sub_catalog);
    
    $html = http_response($sub_catalog["url"]."?rt=nc&_dmd=1&LH_BIN=1");
    $html = htmlspecialchars_decode($html,ENT_QUOTES);
    $reg = "|<span class=\"rcnt\">(.*?)</span>|";
    preg_match ($reg,  
        $html,
        $out);
    $out[1] = str_replace(",", "", $out[1]);
    //echo $out[1]."-----------".$sub_catalog["url"]."?rt=nc&_dmd=1&LH_BIN=1\r\n";
    analyse($html,$_GET["id"],intval($out[1]));
    
    
    $page = 1;
    $skc = 50;
    
    while(($page * $skc) < intval($out[1])){
        $page ++;
        if($page > intval($_GET["p"])) break;
        $html = http_response($sub_catalog["url"]."?rt=nc&_dmd=1&LH_BIN=1&&_pgn=".$page."&_skc=".($skc*($page-1)));
        $html = htmlspecialchars_decode($html,ENT_QUOTES);
        analyse($html,$_GET["id"],intval($out[1]) - $page * $skc - $skc);
    }
    
    close_db();
    return;

}

doit();

echo "ok";

?>
